import { RoleManager } from "./role.manager";

export class CreepManager {

  static run() {
    for (const name in Game.creeps) {
      const creep = Game.creeps[name];
      try {
        this.runCreep(creep);
      } catch (e) {
        console.log(`🚨 ${creep.name} 执行错误: ${e.message}`);
        creep.say('崩溃!');
      }
    }
  }

  private static runCreep(creep: Creep) {
    const roleName = creep.memory.role as RoleType;
    if (!roleName) {
      console.log(`❌ ${creep.name} 未分配角色`);
      creep.say('无角色');
      return;
    }

    const role = RoleManager.getRole(roleName);
    if (!role) {
      console.log(`❌ ${creep.name} 角色 ${roleName} 未注册`);
      creep.say('无配置');
      return;
    }

    const start = Game.cpu.getUsed();
    role.work(creep);
    const cost = Game.cpu.getUsed() - start;

    if (cost > 5) {
      console.log(`⏱️ ${creep.name} 执行耗时: ${cost.toFixed(2)}`);
    }
  }
}